package com.sc.nft.dao;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.OrderSettle;
import com.sc.nft.entity.OrderSplitFlow;
import com.sc.nft.enums.SplitStatusEnum;
import com.sc.nft.mapper.ScOrderSplitFlowMapper;
import com.sc.nft.util.OrderNoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import static com.sc.nft.util.OrderNoUtil.SPLIT_ORDER_PREFIX;


@Slf4j
@Repository
public class OrderSplitFlowDao extends ServiceImpl<ScOrderSplitFlowMapper, OrderSplitFlow> {

    /**
     * 创建分账订单
     *
     * @param orderSettle
     * @return
     */
    public OrderSplitFlow create(OrderSettle orderSettle) {
        OrderSplitFlow orderSplitFlow = new OrderSplitFlow();
        BeanUtil.copyProperties(orderSettle, orderSplitFlow);
        orderSplitFlow.setSplitNo(OrderNoUtil.getOrderSn(SPLIT_ORDER_PREFIX));
        orderSplitFlow.setSplitStatus(SplitStatusEnum.READY);
        orderSplitFlow.setOrderAccountId(orderSettle.getId());
        this.save(orderSplitFlow);
        return orderSplitFlow;
    }

    public void update(OrderSplitFlow orderSplitFlow) {
        this.updateById(orderSplitFlow);
    }

    /**
     * 查询成功的分账单
     * @param orderSettle
     * @return
     */
    public OrderSplitFlow queryByOrderSettle(OrderSettle orderSettle) {
        LambdaQueryWrapper<OrderSplitFlow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderSplitFlow::getOrderAccountId, orderSettle.getId());
        queryWrapper.eq(OrderSplitFlow::getSplitStatus, SplitStatusEnum.SUCCESS);
        return this.getOne(queryWrapper);
    }
}
